package com.yisu.expressway.onedollar.activity;

import java.io.Serializable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yisu.expressway.R;
import com.yisu.expressway.login.LoginManager;
import com.yisu.expressway.onedollar.model.AwardNoticePushObj;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Kaven on 2016/12/27.
 */

public class AwardNoticeDlgActivity extends Activity {
    private static final String EXTRA_KEY_DATA = "extra_key_data";
    @Bind(R.id.iv_goods_cover)
    protected ImageView mGoodsCoverIv;
    @Bind(R.id.tv_goods_name)
    protected TextView mGoodsNameTv;
    private AwardNoticePushObj mData;

    public static void launch(Context context, AwardNoticePushObj awardNoticePushObj) {
        Intent intent = new Intent(context, AwardNoticeDlgActivity.class);
        intent.putExtra(EXTRA_KEY_DATA, awardNoticePushObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @OnClick(R.id.tv_go)
    public void onClick() {
        if (mData == null || mData.objJson == null || mData.objJson.orderId <= 0) {
            return;
        }
        OrderDetailActivity.launch(this, mData.objJson.orderId);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!LoginManager.isLoggedIn()) {
            finish();
            return;
        }
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }
        Serializable serializableExtra = intent.getSerializableExtra(EXTRA_KEY_DATA);
        if (serializableExtra == null) {
            finish();
            return;
        }
        mData = (AwardNoticePushObj) serializableExtra;
        setContentView(R.layout.activity_onedollar_award_notice);
        initView();
    }

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		ButterKnife.bind(this);
	}

    private void initView() {
        if (mData == null || mData.objJson == null) {
            return;
        }
        String goodspic = mData.objJson.goodsPic;
        if (!TextUtils.isEmpty(goodspic)) {
            String[] split = goodspic.split(",");
            if (split.length > 0) {
                String picShow = split[0];
                Glide.with(this).load(picShow).asBitmap().placeholder(R.drawable.pic_loading)
                        .diskCacheStrategy(DiskCacheStrategy.ALL).fitCenter()
                        .animate(R.anim.fade_in_image).into(mGoodsCoverIv);
            }
        } else {
            mGoodsCoverIv.setImageDrawable(getResources().getDrawable(R.drawable.pic_loading));
        }
        mGoodsNameTv.setText(mData.objJson.goodsName);
    }

    public void startTargetActivity(Class targetClass) {
        Intent mIntent = new Intent(getApplicationContext(), targetClass);
        startActivity(mIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
